/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View.reportes;

import Reportes.HoraExtra;
import Reportes.PedidoReporte;
import Presenter.HoraExtraPresenter;
import Presenter.PedidoReportePresenter;
import java.awt.Desktop;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.util.JRLoader;
/**
 *
 * @author Paola
 */
public class Frm_ReporteEnvios extends javax.swing.JDesktopPane {
private List<PedidoReporte> listaPedidoReporte;   
    /**
     * Creates new form Frm_ReporteEnvios
     */
     private DefaultTableModel mdlBuscarVehiculo;
    public Frm_ReporteEnvios() {
        initComponents();
         mdlBuscarVehiculo = new DefaultTableModel();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlBuscarPedido = new javax.swing.JPanel();
        lblIntervaloFec = new javax.swing.JLabel();
        lblDesde = new javax.swing.JLabel();
        lblHasta = new javax.swing.JLabel();
        jdcDesde = new com.toedter.calendar.JDateChooser();
        jdcHasta = new com.toedter.calendar.JDateChooser();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblEnvios = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();

        pnlBuscarPedido.setBorder(javax.swing.BorderFactory.createTitledBorder("Parámetros"));

        lblIntervaloFec.setText("Intervalo de Fechas");

        lblDesde.setText("Desde:");

        lblHasta.setText("Hasta:");

        jButton1.setText("Generar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/botones/pdf.jpg"))); // NOI18N
        jButton2.setMaximumSize(new java.awt.Dimension(30, 30));
        jButton2.setMinimumSize(new java.awt.Dimension(30, 30));
        jButton2.setPreferredSize(new java.awt.Dimension(295, 241));
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton2MouseClicked(evt);
            }
        });
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlBuscarPedidoLayout = new javax.swing.GroupLayout(pnlBuscarPedido);
        pnlBuscarPedido.setLayout(pnlBuscarPedidoLayout);
        pnlBuscarPedidoLayout.setHorizontalGroup(
            pnlBuscarPedidoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlBuscarPedidoLayout.createSequentialGroup()
                .addGroup(pnlBuscarPedidoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblIntervaloFec)
                    .addGroup(pnlBuscarPedidoLayout.createSequentialGroup()
                        .addComponent(lblDesde)
                        .addGap(32, 32, 32)
                        .addComponent(jdcDesde, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(48, 48, 48)
                        .addComponent(lblHasta)
                        .addGap(18, 18, 18)
                        .addComponent(jdcHasta, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton1)))
                .addGap(18, 18, 18)
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 205, Short.MAX_VALUE))
        );
        pnlBuscarPedidoLayout.setVerticalGroup(
            pnlBuscarPedidoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlBuscarPedidoLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(pnlBuscarPedidoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(pnlBuscarPedidoLayout.createSequentialGroup()
                        .addComponent(lblHasta)
                        .addGap(18, 18, 18))
                    .addGroup(pnlBuscarPedidoLayout.createSequentialGroup()
                        .addGroup(pnlBuscarPedidoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(pnlBuscarPedidoLayout.createSequentialGroup()
                                .addComponent(lblIntervaloFec)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(pnlBuscarPedidoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jdcDesde, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jdcHasta, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(lblDesde)
                                    .addComponent(jButton1)))
                            .addGroup(pnlBuscarPedidoLayout.createSequentialGroup()
                                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(7, 7, 7)))
                        .addGap(5, 5, 5))))
        );

        tblEnvios.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "FechaRegistro", "Producto", "Empleado", "Cliente", "Vehiculo", "FechaEntrega", "Estado"
            }
        ));
        jScrollPane1.setViewportView(tblEnvios);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 782, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 762, Short.MAX_VALUE)
                    .addContainerGap()))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 272, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(14, 14, 14)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 247, Short.MAX_VALUE)
                    .addContainerGap()))
        );

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel1.setText("Reporte de envio de Pedidos");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(pnlBuscarPedido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 437, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(18, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 24, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(pnlBuscarPedido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pnlBuscarPedido.getAccessibleContext().setAccessibleName("Parametros");

        getAccessibleContext().setAccessibleName("Parametros");
        getAccessibleContext().setAccessibleParent(this);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
     Date fDesde = jdcDesde.getDate();
        Date fHasta = jdcHasta.getDate();
        mdlBuscarVehiculo.setNumRows(0);
        if(fDesde==null||fHasta==null){
            JOptionPane.showMessageDialog(this, "Ingrese ambas fechas");
            return ;
        }

        if (!fDesde.before(fHasta)){
            JOptionPane.showMessageDialog(this, "Corriga las fechas ingresadas");
            return ;

        }
           mdlBuscarVehiculo.setNumRows(0);
            mdlBuscarVehiculo = (DefaultTableModel) tblEnvios.getModel();
/*
            Object[] row = new Object[]{"Jose Ruiz","12/12/12","2","30","Cancelado"};
            mdlBuscarVehiculo.insertRow(0, row);

            row = new Object[]{"Jose Ruiz","14/12/12","3","45","Pendiente"};
            mdlBuscarVehiculo.insertRow(0, row);
*/
            
                    
    listaPedidoReporte= PedidoReportePresenter.buscarPedidoReporte(fDesde, fHasta);
        
 Object[] row ;
        for (int i = 0; i < listaPedidoReporte.size(); i++) {
            PedidoReporte v = listaPedidoReporte.get(i);
                 
         
          
           
                row = new Object[]{v.getFechaRegistro(),v.getProducto(),v.getEmpleado(),v.getCliente(),v.getVehiculo(),v.getFechaEntrega(),v.getEstado()};
            mdlBuscarVehiculo.insertRow(i, row);
        }
        
            
            
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseClicked

        try
        {
            if(mdlBuscarVehiculo.getRowCount()==0)return;
            if (listaPedidoReporte.size()==0)return;
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-ddHHmmss");
            Date date = new Date();

            String time = sdf.format(date);
            JasperReport reporte = (JasperReport) JRLoader.loadObjectFromFile("src/Reportes/ReportePedido.jasper");
            JasperPrint jasperPrint = JasperFillManager.fillReport(reporte, null, new JRBeanCollectionDataSource(listaPedidoReporte));
            JRExporter exporter = new JRPdfExporter();
            exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
            exporter.setParameter(JRExporterParameter.OUTPUT_FILE, new java.io.File("ReportePedido"+ time  + ".pdf"));
            exporter.exportReport();
            if (Desktop.isDesktopSupported()) {
                try {
                    File myFile = new File("ReportePedido"+ time  + ".pdf");
                    Desktop.getDesktop().open(myFile);
                } catch (Exception ex) {
                    // no application registered for PDFs
                }
            }
        }
        catch(JRException e)
        {
            e.printStackTrace();
        }
    }//GEN-LAST:event_jButton2MouseClicked

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton2ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private com.toedter.calendar.JDateChooser jdcDesde;
    private com.toedter.calendar.JDateChooser jdcHasta;
    private javax.swing.JLabel lblDesde;
    private javax.swing.JLabel lblHasta;
    private javax.swing.JLabel lblIntervaloFec;
    private javax.swing.JPanel pnlBuscarPedido;
    private javax.swing.JTable tblEnvios;
    // End of variables declaration//GEN-END:variables
}
